package com.fishery.customization.dao;

import com.fishery.customization.dao.reposity.BaseDao;
import com.fishery.customization.entity.DragLabel;
import com.fishery.customization.entity.LabelSequence;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class DragLabelDao extends BaseDao<DragLabel> {

	/**
	 * 通过用户id获取用户拖拽后的标签顺序
	 *
	 * @param userId
	 * @return
	 */
	public List<LabelSequence> findLabelSequenceByUserId(String userId) {
		Map<String, Object> fileds = new HashMap<>(2);
		fileds.put("labels_sequence", 1);
		fileds.put("_id", 0);
		DragLabel dragLabel = mongoOperations.findOne(getBasicQuery("user_id", userId, fileds)
				, DragLabel.class, getCollectionName());
		return dragLabel == null ? null : dragLabel.getLabelsSequence();
	}

	/**
	 * 通过用户id保存或修改用户拖拽后的标签顺序
	 *
	 * @param dragLabel
	 * @return
	 */
	public long upsertDragLabelByUserId(DragLabel dragLabel) {
		return mongoOperations.upsert(Query.query(Criteria.where("user_id")
				.is(dragLabel.getUserId())), Update.update("labels_sequence",
				dragLabel.getLabelsSequence()), getCollectionName()).wasAcknowledged() ? 1 : 0;
	}
}
